<template>
  <div>
    <el-dialog
      :title="formId ? '编辑' : '新增'"
      :visible.sync="dialogVisible"
      width="70%"
      :before-close="handleClose"
    >
      <el-form
        :model="dataFrom"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
      >
        <el-form-item label="姓名" prop="name">
          <el-input v-model="dataFrom.name"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select
            v-model="dataFrom.sex"
            placeholder="请选择性别"
            style="width: 100%"
          >
            <el-option label="男" value="1"></el-option>
            <el-option label="女" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="dataFrom.age"></el-input>
        </el-form-item>
        <el-form-item label="证件号码" prop="idCard">
          <el-input v-model="dataFrom.idCard"></el-input>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="dataFrom.phone"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCancle">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addDriver, editDriver, getDriverInfo } from "@/api/files/driver.js";

export default {
  data() {
    return {
      dialogVisible: false,
      formId: undefined,
      dataFrom: {
        name: "",
        sex: "",
        age: "",
        idCard: "",
        phone: "",
      },
      rules: {
        name: [{ required: true, message: "请输入姓名", trigger: "blur" }],
        sex: [{ required: true, message: "请选择性别", trigger: "blur" }],
      },
    };
  },
  methods: {
    open(id) {
      this.formId = id;
      if (id) {
        getDriverInfo({ id }).then((res) => {
          this.dataFrom.name = res.data.name;
          this.dataFrom.sex = res.data.sex;
          this.dataFrom.age = res.data.age;
          this.dataFrom.idCard = res.data.idCard;
          this.dataFrom.phone = res.data.phone;
        });
      }
      this.dialogVisible = true;
    },
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (this.formId) {
            editDriver({ ...this.dataFrom, id: this.formId }).then((res) => {
              this.dialogVisible = false;
              this.$message({
                message: "编辑成功！",
                type: "success",
              });
              this.$emit("success");
              this.resetForm();
            });
          } else {
            addDriver(this.dataFrom).then((res) => {
              this.dialogVisible = false;
              this.$message({
                message: "新增成功！",
                type: "success",
              });
              this.$emit("success");
              this.resetForm();
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm() {
      this.$refs.ruleForm.resetFields();
    },
    handleCancle() {
      this.resetForm();
      this.dialogVisible = false;
    },
    handleClose(done) {
      this.resetForm();
      done();
    },
  },
};
</script>

<style lang="scss" scoped></style>
